package service

import (
    "authserver/global"
    "authserver/model"
    "authserver/utils"
    "bytes"
    "errors"
    "fmt"
    "io"
    "net/http"
)

func DownloadUrl(logId string) (err error ,reader io.Reader) {

    // get source info




    var logInfo model.ParseLog

    global.GVA_DB.Table(logInfo.TableName()).Where("id=?",logId).Find(&logInfo)
    if logInfo.Id==0{
        err = errors.New("下载失败")
        return err ,nil
    }


    var respBody []byte
    err,respBody = utils.GetByte(logInfo.VideoUrl,nil,nil,nil)

    if err!=nil{
        global.GVA_LOG.Error(err.Error())
        return err ,nil
    }

    fmt.Println(len(respBody))
    bf:=bytes.NewBuffer(respBody)
    return nil,bf
}

func DownloadUrlProxy(logId string) (err error ,reader io.Reader,contentLength int64,contentType string) {

    // get source info


    var logInfo model.ParseLog

    global.GVA_DB.Table(logInfo.TableName()).Where("id=?",logId).Find(&logInfo)
    if logInfo.Id==0{
        err = errors.New("下载失败")
        return err ,nil,0,""
    }

    resp,err := http.Get(logInfo.VideoUrl)

    if err!=nil{
        global.GVA_LOG.Error(err.Error())
        return err ,nil,0,""
    }
    fmt.Println(resp.ContentLength)

    return nil,resp.Body,resp.ContentLength,resp.Header.Get("Content-Type")
}
